package com.moleq.adapter;

import java.util.Date;
import java.util.List;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.moleq.dialog.MessageDialog;
import com.moleq.dialog.MessageDialog.OnClickMessageListener;
import com.moleq.itemsetting.tools.MGCreateFiles;
import com.moleq.logic.MoleQConstants;
import com.moleq.mgpos.activities.FunctionSettingActivity;
import com.moleq.mgpos.activities.R;
import com.moleq.models.FunctionPanel;
import com.moleq.models.GlobalInfo;
import com.moleq.service.FunctionPanelService;
import com.moleq.service.ServiceManager;

public class FunctionPanelAdapter extends BaseAdapter {

	public static final String FUNCPANEL_EDITED = "FuncPanelEdit";
	private LayoutInflater mInflater;
	private List<FunctionPanel> listFuncPanel;
	private int selectedRow = 0;
	
	private View ButtonView = null;
	private TextView tv_old_order;
	private EditText et_new_order;
	private Button btn_dialog_ok,btn_dialog_cancel;
	public FunctionSettingActivity fsa;
	private MessageDialog mMessageDialog;
	private boolean result = false;
	private int deleteNO,deleteOrder, oldNO,oldOrder;
	private FunctionPanelService mFunctionPanelService;
	
	Date now = new Date();
	private MGCreateFiles mMGCreateFiles = new MGCreateFiles();
	
	public FunctionPanelAdapter(Context context,List<FunctionPanel> listFuncPanel){
		this.mInflater = LayoutInflater.from(context);
		this.listFuncPanel = listFuncPanel;
		fsa = (FunctionSettingActivity) context;
		mFunctionPanelService = ServiceManager.getInstance().mFunctionPanelService;
	}
	
	
	public void setSource(List<FunctionPanel> listFuncPanel){
		this.listFuncPanel = listFuncPanel;
	}
	
	public List<FunctionPanel> getListFuncPanel(){
		return listFuncPanel;
	}
	
	public int getCount() {
		return listFuncPanel.size();
	}

	public Object getItem(int position) {
		return listFuncPanel.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder= null;
//		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.function_panel_item_list, null);
			holder.btn_order = (Button) convertView.findViewById(R.id.btn_order);
			holder.ll_order = (LinearLayout) convertView.findViewById(R.id.ll_order);
			holder.tv_no = (TextView) convertView.findViewById(R.id.tv_no);
			holder.tv_label = (TextView) convertView.findViewById(R.id.tv_label);
			holder.ll_label = (LinearLayout) convertView.findViewById(R.id.ll_label);
			holder.ibtn_delete = (ImageButton) convertView.findViewById(R.id.ibtn_delete);
			holder.ll_delete = (LinearLayout) convertView.findViewById(R.id.ll_delete);
			
			convertView.setTag(holder);
//		}else {
//			holder = (ViewHolder) convertView.getTag();
//		}
		
		holder.btn_order.setTag(String.valueOf(listFuncPanel.get(position).getTp_no()));
		holder.btn_order.setText(String.valueOf(position + 1));
		holder.tv_no.setText(String.valueOf(listFuncPanel.get(position).getTp_no()));
		holder.tv_label.setText(listFuncPanel.get(position).getTp_label_file());
		holder.ibtn_delete.setTag(String.valueOf(listFuncPanel.get(position).getTp_no()) + "|"
				+ String.valueOf(listFuncPanel.get(position).getTp_order()));
		
		if (position == selectedRow) {
			holder.ll_order.setBackgroundColor(Color.rgb(255,219,16));
			holder.ll_label.setBackgroundColor(Color.rgb(255,219,16));
			holder.ll_delete.setBackgroundColor(Color.rgb(255,219,16));
		} else {
			holder.ll_order.setBackgroundColor(Color.rgb(204, 204, 204));
			holder.ll_label.setBackgroundColor(Color.rgb(204, 204, 204));
			holder.ll_delete.setBackgroundColor(Color.rgb(204, 204, 204));
		}
		
		holder.btn_order.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				ButtonView = v;
				ScrollToRow(position);
				oldNO = listFuncPanel.get(position).getTp_no();
				DialogWindows();
			}
		});
		
		holder.ibtn_delete.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				ImageButton ib = (ImageButton) v.findViewById(R.id.ibtn_delete);
				String temp = ib.getTag().toString();
				String[] strs = temp.split("[|]");
				deleteNO = Integer.valueOf(strs[0].toString());
				deleteOrder = Integer.valueOf(strs[1].toString());
				
				ScrollToRow(position);
				
				int fun_key = listFuncPanel.get(position).getTp_func_key();
				if (fun_key == MoleQConstants.FUNCTION_SIGN_OUT && mFunctionPanelService.getSignOutCount() == 1) {
					Message("Cannot delete the last Sign Out function.", mMessageDialog.MESSAGE_BOX_OK,mMessageDialog.ICON_INFO);
					return;
				}
				Message("Do you want to delete the function?",
						mMessageDialog.MESSAGE_BOX_YES_NO,mMessageDialog.ICON_INFO);
			}
		});
		return convertView;
	}
	
	Dialog DialogWindow = null;
	public void DialogWindows(){
		if (DialogWindow == null) {
			DialogWindow = new Dialog(fsa, R.style.Transparent_dialog);
			DialogWindow.setContentView(R.layout.fc_dialog_perview_models);
			Window win = DialogWindow.getWindow();
			WindowManager.LayoutParams lp = win.getAttributes();
			lp.x= 0;			 
			lp.y= 0;
		}
		tv_old_order = (TextView) DialogWindow.findViewById(R.id.tv_old_order);
		Button b = (Button) ButtonView.findViewById(R.id.btn_order);
		oldOrder = Integer.valueOf(b.getText().toString());
		tv_old_order.setText(String.valueOf(oldOrder));
		
		et_new_order = (EditText) DialogWindow.findViewById(R.id.et_dialog_order);
		et_new_order.setText(null);
		btn_dialog_ok = (Button) DialogWindow.findViewById(R.id.btn_dialog_ok);
		btn_dialog_cancel = (Button) DialogWindow.findViewById(R.id.btn_dialog_cancel);

		btn_dialog_ok.setOnClickListener(OnClickDialogWindowListener);
		btn_dialog_cancel.setOnClickListener(OnClickDialogWindowListener);
		DialogWindow.show();
	}
	
	OnClickListener OnClickDialogWindowListener = new OnClickListener() {
		
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.btn_dialog_ok:
				et_new_order = (EditText) DialogWindow.findViewById(R.id.et_dialog_order);
				String sOrder = et_new_order.getText().toString();
				if (sOrder.equals("0")) {
					Message("Please enter a valid order.",
							mMessageDialog.MESSAGE_BOX_OK,mMessageDialog.ICON_INFO);
					return;
				}
				
				if (!sOrder.equals("")) {
					int newOrder = Integer.valueOf(sOrder);
					int maxOrder = mFunctionPanelService.getMaxPanelOrder();
					if (newOrder > maxOrder) {
						newOrder = maxOrder;
					}
					Button b = (Button) ButtonView.findViewById(R.id.btn_order);
					b.setText(String.valueOf(newOrder));
					
					result = mFunctionPanelService.updateFunctionPanelOrder(newOrder, oldNO);
					if (GlobalInfo.getInstance().isMGNetwork) {
						FunctionPanel functionPanelx = mFunctionPanelService.FindFunctionPanelByNo(oldNO);
						mMGCreateFiles.createrFile(functionPanelx.toMGData("UPDATE"));
					}
					if (result) {
						if (GlobalInfo.getInstance().isMGNetwork) {
							if (newOrder < oldOrder) {
								for (int i = newOrder - 1; i < oldOrder - 1; i++) {
									FunctionPanel fp = listFuncPanel.get(i);
									fp.setTp_order(fp.getTp_order() + 1);
									mMGCreateFiles.createrFile(listFuncPanel.get(i).toMGData("UPDATE"));
								}
							}else if (newOrder > oldOrder) {
								for (int i = newOrder - 1; i >= oldOrder; i--) {
									FunctionPanel fp = listFuncPanel.get(i);
									fp.setTp_order(fp.getTp_order() - 1);
									mMGCreateFiles.createrFile(listFuncPanel.get(i).toMGData("UPDATE"));
								}
							} 
						}
						mFunctionPanelService.postUpdateSetFunctionPanelOrder(newOrder, oldOrder, oldNO);
						fsa.ShowMessage(result, 1);
						listFuncPanel = mFunctionPanelService.getAllFunctionPanel();
						setSource(listFuncPanel);
						notifyDataSetChanged();
						if (newOrder > 0) {
							ScrollToRow(newOrder - 1);
							FunctionSettingActivity.set_lv_func_panel_list_Selection(newOrder - 1);
						}else {
							ScrollToRow(0);
							FunctionSettingActivity.set_lv_func_panel_list_Selection(0);
						}
					}
				}
				fsa.Keyboard();
				DialogWindow.dismiss();
				break;
			case R.id.btn_dialog_cancel:
				fsa.Keyboard();
				DialogWindow.dismiss();
				break;
			}
		}
	};
	
	public final class ViewHolder{
		public Button btn_order;
		public TextView tv_no;
		public TextView tv_label;
		public ImageButton ibtn_delete;
		public LinearLayout ll_order;
		public LinearLayout ll_label;
		public LinearLayout ll_delete;
	}
	
	public void ScrollToRow(int row) {
		selectedRow = row;
		this.notifyDataSetChanged();
	}
	public int GetSelectedIndex(){
		return selectedRow;
	}
	
	private OnClickMessageListener onClickMessageListener = new OnClickMessageListener(){

		public void onClickYes() {
			// TODO Auto-generated method stub
			int index = GetSelectedIndex();
			
			result = mFunctionPanelService.deleteFunctionPanelByNo(deleteNO);
			if (GlobalInfo.getInstance().isMGNetwork) {
				mMGCreateFiles.createrFile(listFuncPanel.get(index).toMGData("DELETE"));
			}
			if (result) {
				mFunctionPanelService.postDeleteSetFunctionPanelOrder(deleteOrder);
				if (GlobalInfo.getInstance().isMGNetwork) {
					for (int i = index + 1; i < listFuncPanel.size(); i++) {
						FunctionPanel fp = listFuncPanel.get(i);
						fp.setTp_order(fp.getTp_order() - 1);
						mMGCreateFiles.createrFile(fp.toMGData("UPDATE"));
					}
				}
				fsa.ShowMessage(result, 2);
				listFuncPanel = mFunctionPanelService.getAllFunctionPanel();
				setSource(listFuncPanel);
				notifyDataSetChanged();
				ScrollToRow(0);
				FunctionSettingActivity.set_lv_func_panel_list_Selection(0);
			}
			mMessageDialog.dismiss();
		}

		public void onClickNo() {
			// TODO Auto-generated method stub
			mMessageDialog.dismiss();
		}

		public void onClickOK() {
			// TODO Auto-generated method stub
			mMessageDialog.dismiss();
		}
		
	};
	
	public void Message(String text,int type,int icon){
		mMessageDialog = new MessageDialog(fsa);
		mMessageDialog.setStyle(type);
		mMessageDialog.setMessageIcon(icon);
		mMessageDialog.setMessage(text);
		mMessageDialog.setOnClickMessageListener(onClickMessageListener);
		mMessageDialog.show();
	}
}
